package com.tnxst.phexamination.adapter.follow;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.dto.TCMHealthDTO;
import com.tnxst.phexamination.fragment.RecordTcmHealthElderlyFragment;
import com.tnxst.phexamination.fragment.follow.HypertensionFollowFragment;
import com.tnxst.phexamination.utils.StringUtils;

import java.util.List;

public class TcmHealthFollowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private FragmentManager fragmentManager;

    private List<TCMHealthDTO> tcmHealthDTOS;

    private RecordTcmHealthElderlyFragment fragment;



    public TcmHealthFollowAdapter(List<TCMHealthDTO> tcmHealthDTOS, FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        this.tcmHealthDTOS = tcmHealthDTOS;
    }

    public void setTcmHealthDTOS(List<TCMHealthDTO> tcmHealthDTOS) {
        this.tcmHealthDTOS = tcmHealthDTOS;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view =  LayoutInflater.from(context).inflate(R.layout.tcm_health_follow_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holders = (ViewHolder) holder;
        TCMHealthDTO tcmHealthDTO = tcmHealthDTOS.get(position);

        holders.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(tcmHealthDTO);
            }
        });

        if (StringUtils.isNotNull(tcmHealthDTO.getPersonName()))
            holders.patientName.setText(tcmHealthDTO.getPersonName());

        if (StringUtils.isNotNull(tcmHealthDTO.getDoctorName()))
            holders.doctorName.setText(tcmHealthDTO.getDoctorName());

        if (StringUtils.isNotNull(tcmHealthDTO.getFollowUpDate()))
            holders.followDate.setText(tcmHealthDTO.getFollowUpDate());

        if (StringUtils.isNotNull(tcmHealthDTO.getNextFollowUpDate()))
            holders.nextFollowDate.setText(tcmHealthDTO.getNextFollowUpDate());

        if (StringUtils.isNotNull(tcmHealthDTO.getTcHealthHighScore())){
            String followType = tcmHealthDTO.getTcHealthHighScore();
            switch (followType){
                case "Peaceful":
                    holders.followType.setText("平和质");
                    break;
                case "Yang":
                    holders.followType.setText("阳虚质");
                    break;
                case "Phlegm":
                    holders.followType.setText("痰湿质");
                    break;
                case "Qi":
                    holders.followType.setText("气虚质");
                    break;
                case "Yin":
                    holders.followType.setText("阴虚质");
                    break;
                case "Hot":
                    holders.followType.setText("湿热质");
                    break;
                case "Blood":
                    holders.followType.setText("血瘀质");
                    break;
                case "StQi":
                    holders.followType.setText("气郁质");
                    break;
                case "Specific":
                    holders.followType.setText("特禀质");
                    break;
            }
        }


    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View view;

        TextView patientName;

        TextView doctorName;

        TextView followDate;

        TextView nextFollowDate;

        TextView followType;



        public ViewHolder(View view) {
            super(view);
            this.view = view;
            this.patientName = view.findViewById(R.id.textView166);
            this.doctorName = view.findViewById(R.id.textView168);
            this.followDate = view.findViewById(R.id.textView276);
            this.nextFollowDate = view.findViewById(R.id.textView305);
            this.followType = view.findViewById(R.id.textView171);
        }
    }

    @Override
    public int getItemCount() {
        return tcmHealthDTOS.size();
    }


    private void showFragment(TCMHealthDTO tcmHealthDTO) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(R.anim.fragment_slide_up, 0);
        fragment = new RecordTcmHealthElderlyFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("tcmHealthDTO", tcmHealthDTO);
        fragment.setArguments(bundle);
        ft.replace(android.R.id.content, fragment);
        ft.show(fragment);
        ft.commitAllowingStateLoss();
    }

}
